var _contextPath;
$(document).ready(function() {
	_contextPath = $("#contextPath").val();
	merchantlist();
});

function getContextPath() {
	if ("" == _contextPath) {
		_contextPath = $("#contextPath").val();
	}
	return _contextPath;
}


var isNum = function(str) {
	return !isNaN($.trim(str));
};

var isInt = function(str) {
	return /^\d+$/.test(str);
};

var isDate = function(str) {
	return /^\d{2}[01]\d[0123]\d[012]\d[0123455]\d$/
			.test(str);
};

var isNull = function(str) {
	return $.trim(str) == "";
};

/**
 * 入库卡列表
 */
function merchantlist() {
	$("#merchantId").empty();
	$.ajax({
		type : 'POST',
		url : _contextPath + 'iccard/combocard/merchantlist',
		dataType : 'json',
		success : function(list) {
			if (list != null) {
				for ( var i = 0; i < list.length; i++) {
					var option = '<option value="' + list[i].merchantId
							+ '">' + list[i].merchantId +"  "+ list[i].merchantName + '</option>';
					$("#merchantId").append(option);
				}
			}
		}
	});
}

function addm(){
	var length = $("#merctDiv .panel > div").size();
	var $cmtemp= $("#merchantTemp_0").clone();
	$cmtemp.attr("id","merchantTemp_"+length);
	$("pp",$cmtemp).html(length+1);
	$("select,input",$cmtemp).val("");
	$("select,input",$cmtemp).each(function(index) { 
		if(index==0){
			$(this).attr("name","comboCardProductDetails["+length+"].merchantId");
		}else if(index==1){
			$(this).attr("name","comboCardProductDetails["+length+"].productNames");
		}else if(index==2){
			$(this).attr("name","comboCardProductDetails["+length+"].productOrignalPrice");
		}else if(index==3){
			$(this).attr("name","comboCardProductDetails["+length+"].productFavorablePrice");
		}else if(index==4){
			$(this).attr("name","comboCardProductDetails["+length+"].productSalePrice");
		}else if(index==5){
			$(this).attr("name","comboCardProductDetails["+length+"].productCopies");
		}else if(index==6){
			$(this).attr("name","comboCardProductDetails["+length+"].productRaisePriceRate");
		}
	})
	$("#merctDiv .panel").append($cmtemp);
}
var replaceAll = function(s1,s2) { 
    return this.replace(new RegExp("\"","gm")); 
}

function delm(obj){
	var length = $("#merctDiv .panel > div").size();
	if(length>1){
		//console.info($(obj).parents("tr").parent().parent().parent());
		$(obj).parents("tr").parent().parent().parent().remove();
		$("#merctDiv .panel > div").each(function(i) {
			$(this).attr("id","merchantTemp_"+i);
			$("select,input",$(this)).each(function(index) { 
				if(index==0){
					$(this).attr("name","comboCardProductDetails["+i+"].merchantId");
				}else if(index==1){
					$(this).attr("name","comboCardProductDetails["+i+"].productNames");
				}else if(index==2){
					$(this).attr("name","comboCardProductDetails["+i+"].productOrignalPrice");
				}else if(index==3){
					$(this).attr("name","comboCardProductDetails["+i+"].productFavorablePrice");
				}else if(index==4){
					$(this).attr("name","comboCardProductDetails["+i+"].productSalePrice");
				}else if(index==5){
					$(this).attr("name","comboCardProductDetails["+i+"].productCopies");
				}else if(index==6){
					$(this).attr("name","comboCardProductDetails["+i+"].productRaisePriceRate");
				}
			})
		});
	}else{
		top.uppShow("至少保证有一个商家参加套餐");
	}
}

function saveCombo() {
	$("#savab").attr('disabled', 'disabled');
	_contextPath =$("#contextPath").val();
	if ($("#comboCardId").val())
		url = _contextPath + "iccard/combocard/update?rid=" + Math.random();
	else {
		url = _contextPath + "iccard/combocard/add?rid=" + Math.random();
	}
	var data = $("#combocard_form").serializeArray();
	alert(data);
	if (!$("#combocard_form").form('validate')){
		return false;
	}
	$('table input,textarea').attr('disabled', 'disabled');
	$.ajax({
		type : 'POST',
		url : url,
		data : data,
		dataType : 'json',
		success : function(root) {
			top.uppShow(root.msgDesc);
			// 隐藏保存按钮
			$("#btn_div").hide();
			$("#savab").attr('disabled', '');
		},
		error : function(data) {
			uppAjaxError(data);
			$("#savab").attr('disabled', '');
		}
	});
}

function clearForm(){  
    $('#combocard_form').form('clear');  
}


function merchantChange(obj){
	if(!isNum($(obj).val())){
		top.uppShow("请输入数字");
		$(obj).focus();
	}else{
		var yj = parseInt($("input",$(obj).parents("table")).eq(1).val());
		var yh = parseInt($("input",$(obj).parents("table")).eq(2).val());
		var xs = parseInt($("input",$(obj).parents("table")).eq(3).val());
		//var zfs = parseInt($("input",$(obj).parents("table")).eq(4).val());
		
		var zfs=0;
		$("input[name*='productCopies']").each(function(index) { 
			if($(this).val()==""){
				return ;
			}else{
				if(parseInt($(this).val())<zfs || zfs==0){
					zfs = parseInt($(this).val());
				}
			}
		});
		if(isNum(zfs)){
			$("#comboCardTotalCopies").val(zfs);
		}
		
		if(isNum(yh) && xs!=0 && yh!=0){
			var jabfb = (xs-yh)/yh;
			if(isNum(jabfb)){
				$("input",$(obj).parents("table")).last().val(Percentage(jabfb));
			}
		}
		
		var yzjg=0;
		$("input[name*='productOrignalPrice']").each(function(index) { 
			if($(this).val()==""){
				return ;
			}else{
				yzjg = yzjg+parseInt($(this).val());
			}
		});
		if(isNum(yzjg)){
			$("#comboCardOrignalPrice").val(yzjg);
		}
		
		var zyhjg=0;
		$("input[name*='productFavorablePrice']").each(function(index) { 
			if($(this).val()==""){
				return ;
			}else{
				zyhjg = zyhjg+parseInt($(this).val());
			}
		});
		if(isNum(zyhjg)){
			$("#comboCardFavorablePrice").val(zyhjg);
		}
		
		var zxsjg=0;
		$("input[name*='productSalePrice']").each(function(index) { 
			if($(this).val()==""){
				return ;
			}else{
				zxsjg = zxsjg+parseInt($(this).val());
			}
		});
		if(isNum(zxsjg)){
			$("#comboCardSalePrice").val(zxsjg);
		}
		
		if(isNum(zyhjg) && zyhjg!=0 && zxsjg!=0){
			var zjabfb = (zxsjg-zyhjg)/zyhjg;
			if(isNum(zjabfb)){
				$("#comboCardRaisePriceRate").val(Percentage(zjabfb));
			}
		}
		
		if(isNum(yzjg) && yzjg!=0 && zxsjg!=0){
			var zjk = zxsjg/yzjg;
			if(isNum(zjk)){
				$("#totalDiscount").val(Percentage(zjk));
			}
		}
		
	}
}

function Percentage(num) {
    return (Math.round(num * 10000) / 100.00); 小数点后两位百分比
}